// litgroups.cpp
// Customizer
// Protea Customizer / Business Logic for managing literal groups
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "customizer.h"


//////////////////////////////////////////////////////////////////////////////
//
// LIT_GROUP Transactions
//
//////////////////////////////////////////////////////////////////////////////

//@ CHA Add Literal Group
//@ COD AddLiteralGroup
//@ NUM 28
//@ CRI LIT_GROUP_NAME
//@ TAB LIT_GROUP Write
//
//@ GEN Add Literal Group will add a literal group for an application
//
TRANSACTION_CODE( AddLiteralGroup )
{
	//@ GRS Start the transaction
	START_TXN( AddLiteralGroup );
		START_DB_TXN();
			AddLiteralGroupInternal( &txnvars, false );
		END_DB_TXN();

		LOG_TRANSACTION();
	CLEANUP_TXN();
	END_TXN();
	//@ GRE End the transaction
};



//@ CHA Delete Literal Group
//@ COD DeleteLiteralGroup
//@ NUM 29
//@ CRI LIT_GROUP_NAME
//@ TAB LIT_GROUP Write
//
//@ GEN Delete Literal Group will delete a literal group for an application
//
TRANSACTION_CODE( DeleteLiteralGroup ) 
{
	//@ GRS Start the transaction
	START_TXN( DeleteLiteralGroup );
		START_DB_TXN();
			DelLiteralGroupInternal( &txnvars, false );
		END_DB_TXN();

		LOG_TRANSACTION();
	CLEANUP_TXN();
	END_TXN();
	//@ GRE End the transaction
};



//@ CHA Change Literal Group
//@ COD ChangeLiteralGroup
//@ NUM 29
//@ CRI LIT_GROUP_NAME
//@ TAB LIT_GROUP Write
//
//@ GEN Change Literal Group will alter a literal group for an application
//
TRANSACTION_CODE( ChangeLiteralGroup ) {
	//@ GRS Start the transaction
	bool require_log = false;
	START_TXN( ChangeLiteralGroup );
		START_DB_TXN();
			require_log = ChgLiteralGroupInternal( &txnvars, false );
		END_DB_TXN();

		if (require_log)
			LOG_TRANSACTION();
	CLEANUP_TXN();
	END_TXN();
	//@ GRE End the transaction
};



TRANSACTION_CODE( ReviewLitGroups ) {
	XCBuffer* TransBuf = NULL;
	XCBuffer* detail = NULL;
	XCDBStatement* stmt = NULL;

	//@ GRS Start the transaction
	START_TXN( ReviewLitGroups );
		XCSQLGen* query = txnvars.query;

		//@ Create transaction defn buffer
		TransBuf = App->CreateBuffer( FMT_LIT_GROUP, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			int child = txnvars.buffer->GetChildFormat();
			if (child) {
				XCOrder ord;
				detail = App->CreateBuffer( child, true );

				// Formulate select statement
				query->MakeSELECT( TransBuf );

				// If Appl_nbr = 0, select all records
				XCCompare comptemp;
				XCField* temp = TransBuf->GetField( FLD_APPL_NBR );
				if (temp->Compare(0) != 0)
					query->AddWHERE( TransBuf->GetField( FLD_APPL_NBR ), &comptemp );
				query->AddORDERBY( FLD_LIT_GROUP_NAME, &ord );

				// Execute the query
				stmt = txnvars.dbtxn->ExecuteSQL( query );

				while (!stmt->Eof()) {
					detail->MoveCorresponding( TransBuf, txnvars.Errors, true );

					// Send back a response
					App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
					stmt->MoveNext();
				};
			};
		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 3, &TransBuf, &stmt, &detail );
	END_TXN();
	//@ GRE End the transaction
};




TRANSACTION_CODE( LitGroupWhereUsed ) {
	XCBuffer* TransBuf = NULL;
	XCBuffer* Lit_Group = NULL;
	XCBuffer* detail = NULL;
	XCDBStatement* stmt = NULL;

	XCField* temp;

	//@ GRS Start the transaction
	START_TXN( LitGroupWhereUsed );
		//@ Create transaction defn buffer
		TransBuf = App->CreateBuffer( FMT_DATA_STRUCTURES, false );

		//@ Create a LIT_GROUP buffer
		Lit_Group = App->CreateBuffer( FMT_LIT_GROUP, false );

		//@ Move screen info to transaction buffers
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		Lit_Group->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			//@ Ensure that LIT_GROUP_NAME is not blank
			temp = txnvars.buffer->GetField( FLD_LIT_GROUP_NAME );
			if (!temp || temp->IsBlank())
				txnvars.Errors->Add( ERR_IS_BLANK, SEV_CRITICAL, FLD_LIT_GROUP_NAME, 1, temp );

			//@ SUB LookupNumberFromName Convert LIT_GROUP_NAME to LIT_GROUP_NBR
			if (!LookupNumberFromName( &txnvars, txnvars.buffer, Lit_Group, FLD_LIT_GROUP_NBR, FLD_LIT_GROUP_NAME, FMT_LIT_GROUP ))
				txnvars.Errors->Add( ERR_DOESNT_EXIST, SEV_CRITICAL, FLD_LIT_GROUP_NAME, 1, ERR_LIT_GROUP );

			//@ Move LIT_GROUP_NBR to DECIMAL_PLACES
			TransBuf->MoveUnlikeField( FLD_DECIMAL_PLACES, Lit_Group, FLD_LIT_GROUP_NBR );

			int child = txnvars.buffer->GetChildFormat();
			if (child) {
				detail = App->CreateBuffer( child, true );

				// Find out which literals are in the literal group
				stmt = GetLitGroupWhereUsed( TransBuf, &txnvars );
				while (!stmt->Eof()) {
					txnvars.Errors->NextBuffer();
					detail->MoveCorresponding( TransBuf, txnvars.Errors, true );

					//@ Convert FIELD_NUMBER into FIELD_NAME
					if (detail->GetField( FLD_FIELD_NAME ))
						LookupNameFromNumber( &txnvars, TransBuf, detail, FLD_FIELD_NUMBER, FLD_FIELD_NAME, FMT_FIELD_DESCRIPTOR );

					//@ Convert FORMAT_NUMBER into FORMAT_NAME
					LookupNameFromNumber( &txnvars, TransBuf, detail, FLD_FORMAT_NUMBER, FLD_FORMAT_NAME, FMT_FORMAT_DESCRIPTR );
					txnvars.Errors->Check();

					App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
					stmt->MoveNext();
				};
			};
		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 4, &TransBuf, &Lit_Group, &stmt, &detail );
	END_TXN();
	//@ GRE End the transaction
};
